package com.xye.auth.base.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.password.PasswordEncoder;


/**
 * 密码校验器<br>
 *     默认的MD5加密 后续更新为其他加密方式 切换一下 就行
 * 2018.08.01
 *
 * @author lxl
 */
@Configuration
public class PasswordEncoderConfig {
	/**
	 * 单点登录密码 ScMc2_*0_GS1_MsS9_#= MD5加密后: 205F22D377B26A8BA9F81E557B8FBC44
	 */
	@Value("${oauth2.sso-password:205F22D377B26A8BA9F81E557B8FBC44}")
	private String ssoPassword;
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new PasswordEncoder() {
			
			@Override
			public boolean matches(CharSequence rawPassword, String encodedPassword) {
				//如果当前用户是来源于单点登录的 ,在此处就需要单点处理一下
				if(ssoPassword.equals(Md5Util.encode((String)rawPassword))){
					return  true;
				}
				return Md5Util.encode((String) rawPassword).equals(encodedPassword);
			}
			
			@Override
			public String encode(CharSequence rawPassword) {
				return (String)rawPassword;
			}
		};
    }
}
